package com.xqh.controller.ques;

import com.xqh.commom.BaseController;
import com.xqh.commom.SystemUserUtils;
import com.xqh.base.mybatis.page.Page;
import com.xqh.base.util.id.IdWorker;
import com.xqh.base.util.response.RespMsg;
import com.xqh.common.fastdfs.FastDFSFile;
import com.xqh.common.fastdfs.FileUploadUtils;
import com.xqh.entity.ques.ImportRecord;
import com.xqh.entity.ques.QuesType;
import com.xqh.entity.ques.QuestionTmp;
import com.xqh.service.ques.ImportRecordService;
import com.xqh.service.ques.QuesTypeService;
import com.xqh.service.ques.QuestionTmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目相关
 */
@Controller
@RequestMapping(value = "/ques/content")
public class QuesContentController extends BaseController{

    @Autowired
    private ImportRecordService importRecordService;

    @Autowired
    private QuesTypeService quesTypeService;

    @Autowired
    private QuestionTmpService tmpService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(){
        return new ModelAndView("ques/content/ques_content_list");
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    public @ResponseBody Page listData(Page page, ImportRecord dto){
       importRecordService.findByPage(page);
       return page;
    }

    /**
     * 题目内容上传页面
     * @return
     */
    @RequestMapping(value = "toUpload", method = RequestMethod.GET)
    public ModelAndView toUpload(ModelMap model){
        Map<String,Object> map = new HashMap<>();
        List<QuesType> firstList = quesTypeService.findByBiz(map);
        model.put("typeList", firstList);
        return new ModelAndView("ques/content/ques_content_upload");
    }

    /**
     * 内容上传
     * @return
     */
    @RequestMapping(value = "uploadContent")
    public @ResponseBody
    RespMsg<?> uploadContent(@RequestParam("file") MultipartFile file){
        String code = IdWorker.getStringCode();
        String path = FileUploadUtils.uploadFileLocal(file, code);
        if(StringUtils.hasText(path)){
            ImportRecord re = new ImportRecord();
            re.setFileName(file.getOriginalFilename());
            re.setCode(code);
            re.setFilePath(path);
            re.setCreateBy(SystemUserUtils.getCurrentUser().getUsername());
            re.setCreateDate(new Date());
            importRecordService.saveData(re);
            return RespMsg.successResp(new FastDFSFile(code));
        }
        return RespMsg.failResp(new FastDFSFile("fail"));
    }

    @RequestMapping(value = "saveContentUpload", method = RequestMethod.POST)
    public @ResponseBody RespMsg saveContentUpload(QuestionTmp tmp){
        try {
            tmpService.saveData(tmp);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp();
    }
}
